package io.xccit.hotelsystem.ui.hotel.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.List;

import io.xccit.hotelsystem.R;
import io.xccit.hotelsystem.ui.home.HomeFragment;

/**
 * 酒店页面酒店适配器
 */
public class HotelAdapter extends RecyclerView.Adapter<HotelAdapter.ViewHolder> {
    public OnItemClickListener getListener() {
        return listener;
    }

    public interface OnItemClickListener {
        void onItemClick(int hotelId);
    }

    private static List<HomeFragment.Hotel> hotelList;
    private OnItemClickListener listener;

    public HotelAdapter(List<HomeFragment.Hotel> hotelList, OnItemClickListener listener) {
        this.hotelList = hotelList;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_hotel_with_desc, parent, false);
        return new ViewHolder(view, this);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        HomeFragment.Hotel hotel = hotelList.get(position);
        holder.hotelName.setText(hotel.getName());
        holder.hotelDesc.setText(hotel.getDesc());
        holder.hotelPrice.setText("¥" + hotel.getPrice());
        Glide.with(holder.itemView.getContext())
                .load(hotel.getImageRes())
                .into(holder.hotelImage);
    }

    @Override
    public int getItemCount() {
        return hotelList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private HotelAdapter adapter;
        ImageView hotelImage;
        TextView hotelName;
        TextView hotelDesc;
        TextView hotelPrice;

        public ViewHolder(@NonNull View itemView, HotelAdapter adapter) {
            super(itemView);
            this.adapter = adapter;
            hotelImage = itemView.findViewById(R.id.hotel_image);
            hotelName = itemView.findViewById(R.id.hotel_name);
            hotelDesc = itemView.findViewById(R.id.hotel_desc);
            hotelPrice = itemView.findViewById(R.id.hotel_price);
            
            itemView.setOnClickListener(v -> {
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION && adapter.listener != null) {
                    adapter.listener.onItemClick(hotelList.get(position).getId());
                }
            });
        }
    }

    public void setHotelList(List<HomeFragment.Hotel> hotelList) {
        this.hotelList = hotelList;
        notifyDataSetChanged();
    }
}